home *** CD-ROM | disk | FTP | other *** search
/ Aminet 22 / Aminet 22 (1997)(GTI - Schatztruhe)[!][Dec 1997].iso / Aminet / dev / e / amigae33a.lha / E_v3.3a / Src.lha / Src / Pd / threads / Thread2.e < prev   
Text File  |  1995-04-11  |  2KB  |  94 lines

  1. -> thread2.e - a safer way to implement threads than thread.e
  2.  
  3. MODULE 'dos/dos'
  4. MODULE 'dos/dostags'
  5. MODULE 'dos/dosextens'
  6. MODULE 'utility/tagitem'
  7.  
  8. MODULE 'exec/nodes'
  9. MODULE 'exec/ports'
  10. MODULE 'exec/memory'
  11.  
  12. MODULE '*modules/geta4'
  13.  
  14. PROC main()
  15.   -> message for interprocess communication
  16.   DEF message:PTR TO mn
  17.   -> port of main process
  18.   DEF port:PTR TO mp
  19.   -> pointer to the thread process
  20.   DEF mythread:PTR TO process
  21.  
  22.   storea4()
  23.  
  24.   -> port to talk with thread
  25.   IF port:=CreateMsgPort()
  26.  
  27.     -> allocate message
  28.     IF message:=AllocMem(SIZEOF mn,MEMF_CLEAR OR MEMF_PUBLIC)
  29.  
  30.       -> fill in message node
  31.       message::ln.type:=NT_MESSAGE
  32.       message.length:=SIZEOF mn
  33.       message.replyport:=port
  34.  
  35.       -> create a thread process
  36.       IF mythread:=CreateNewProc(
  37.         [
  38.         NP_ENTRY,{thread}, -> where the thread process begins
  39.         NP_NAME,'MyThread', -> the thread process name
  40.         TAG_DONE
  41.         ])
  42.  
  43.         -> send the thread a startup message
  44.         PutMsg(mythread.msgport,message)
  45.  
  46.         /* main program here */
  47.  
  48.        -> wait for the threads' death
  49.         WaitPort(port)
  50.  
  51.       ENDIF
  52.  
  53.       FreeMem(message,SIZEOF mn)
  54.     ENDIF
  55.     DeleteMsgPort(port)
  56.   ENDIF
  57. ENDPROC
  58.  
  59. PROC thread()
  60.   -> pointer to this process
  61.   DEF thisthread:PTR TO process
  62.   -> pointer to the received message
  63.   DEF message:PTR TO mn
  64.  
  65.   -> get the global data pointer, previously stored by the main process.
  66.   -> IMPORTANT: do this before using global variables or functioncalls.
  67.   geta4()
  68.  
  69.   -> find out about ourselves
  70.   thisthread:=FindTask(0)
  71.  
  72.   -> wait for the startup message (sent by the main process)
  73.   WaitPort(thisthread.msgport)
  74.  
  75.   -> get the startup message
  76.   message:=GetMsg(thisthread.msgport)
  77.  
  78.   /* thread program begins here */
  79.  
  80.   -> useless program, just waits a second
  81.   PrintF('Hello, I\am a newbie thread. It\as nice to be here.\n')
  82.   Delay(50)
  83.  
  84.   /* thread program ends here */
  85.  
  86.   -> make sure there is no taskswitching after we replied the message
  87.   Forbid()
  88.  
  89.   -> reply to main process
  90.   ReplyMsg(message)
  91.  
  92. -> thread dies here
  93. ENDPROC
  94.